package cn.enilu.flash.service.shop;


import cn.enilu.flash.bean.entity.shop.Goods;
import cn.enilu.flash.bean.entity.shop.GoodsSku;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.bean.vo.shop.GoodsVo;
import cn.enilu.flash.dao.shop.GoodsRepository;
import cn.enilu.flash.dao.system.FileInfoRepository;
import cn.enilu.flash.service.BaseService;
import cn.enilu.flash.utils.Lists;
import cn.enilu.flash.utils.factory.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsService extends BaseService<Goods,Long,GoodsRepository>  {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private GoodsRepository goodsRepository;
    @Autowired
    private GoodsSkuService goodsSkuService;
    @Autowired
    private FileInfoRepository fileInfoRepository;

    @Override
    public void deleteById(Long id) {
        Goods goods = get(id);
        goods.setIsDelete(true);
        update(goods);
    }
    public GoodsVo getDetail(Long id){
        Goods goods = get(id);
        List<GoodsSku> skuList = goodsSkuService.queryAll(Lists.newArrayList(
                SearchFilter.build("idGoods",id),
                SearchFilter.build("isDeleted",false)
        ));
        GoodsVo vo = new GoodsVo();
        vo.setGoods(goods);
        vo.setSkuList(skuList);
        return vo;
    }

    /**
     * 商品上架或者下架
     * @param id
     * @param isOnSale
     */
    public void changeIsOnSale(Long id, Boolean isOnSale) {
        Goods goods = get(id);
        goods.setIsOnSale(isOnSale);
        update(goods);
    }


    public Page getGoodsPriceList(Long categoryId, String name, Page page){
        String sql = "select * from \n" +
                "(\n" +
                "\tselect \n" +
                "\tA.id as goodId,\n" +
                "\tA.name,\n" +
                "\tA.id_category as categoryId,\n" +
                "\t(select name from  t_shop_category where id = A.id_category) as categoryName,\n" +
                "\tA.is_on_sale  as isOnSale,\n" +
                "\tA.stock,\n" +
                "\tA.price,\n" +
                "\tA.discount_desc as discountDesc ,\n" +
                "\tA.discount ,\n" +
                "\tA.discount_price as discountPrice,\n" +
                "\tA.discount_price_start as discountPriceStart,\n" +
                "\tA.discount_price_end as discountPriceEnd,\n" +
                "\t'' as code,\n" +
                "\t'' as codeName,\n" +
                "\tnull as skuId\n" +
                "\tfrom t_shop_goods A\n" +
                "\twhere A.disable  = 0\n" +
                "\tand A.is_delete  = 0\n" +
                "\tand (A.id_category = :categoryId or :categoryId is null)\n" +
                "\tand A.name like concat('%',:name,'%') \n" +
                "\tunion\n" +
                "\tselect \n" +
                "\tA.id as goodId,\n" +
                "\tA.name,\n" +
                "\tA.id_category as categoryId,\n" +
                "\t(select name from  t_shop_category where id = A.id_category) as categoryName,\n" +
                "\tA.is_on_sale  as isOnSale,\n" +
                "\tB.stock,\n" +
                "\tB.price,\n" +
                "\tB.discount_desc as discountDesc ,\n" +
                "\tB.discount ,\n" +
                "\tB.discount_price as discountPrice,\n" +
                "\tB.discount_price_start as discountPriceStart,\n" +
                "\tB.discount_price_end as discountPriceEnd,\n" +
                "\tB.code,\n" +
                "\tB.code_name as codeName,\n" +
                "\tB.id as skuId\n" +
                "\tfrom t_shop_goods A\n" +
                "\tjoin t_shop_goods_sku B on A.id  = B.id_goods\n" +
                "\twhere A.disable  = 0\n" +
                "\tand A.is_delete  = 0\n" +
                "\tand B.disable  = 0\n" +
                "\tand (A.id_category = :categoryId or :categoryId is null)\n" +
                "\tand A.name like concat('%',:name,'%') \n" +
                ") T1\n" +
                "order by T1.name  ";

        Map<String,Object> params = new HashMap<>();
        params.put("name",name);
        params.put("categoryId",categoryId);

        return goodsRepository.queryBySql(sql,params,page);
    }
}

